home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / colorwheel / WheelGrad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  8.0 KB  |  215 lines

  1. /*
  2. COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1999
  3. Amiga, Inc.  All rights reserved.
  4.  
  5. DISCLAIMER: This software is provided "as is".  No representations or
  6. warranties are made with respect to the accuracy, reliability, performance,
  7. currentness, or operation of this software, and all use is at your own risk.
  8. Neither Amiga nor the authors assume any responsibility or liability
  9. whatsoever with respect to your use of this software.
  10. */
  11.  
  12. /* WheelGrad.c - simple example of colorwheel and gradient slider
  13.  *
  14.  * Puts up a colorwheel and gradient slider and changes the gradient slider
  15.  * color based on where the colorwheel knob is moved.
  16.  */
  17.  
  18. #include <exec/types.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/intuitionbase.h>
  21. #include <intuition/screens.h>
  22. #include <graphics/displayinfo.h>
  23. #include <intuition/gadgetclass.h>
  24. #include <gadgets/colorwheel.h>
  25. #include <gadgets/gradientslider.h>
  26.  
  27. #include <clib/intuition_protos.h>
  28. #include <clib/exec_protos.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/graphics_protos.h>
  31. #include <clib/colorwheel_protos.h>
  32.  
  33. #include <pragmas/intuition_pragmas.h>
  34. #include <pragmas/exec_pragmas.h>
  35. #include <pragmas/dos_pragmas.h>
  36. #include <pragmas/graphics_pragmas.h>
  37. #include <pragmas/colorwheel_pragmas.h>
  38.  
  39. #include <stdio.h>
  40.  
  41.  
  42. /****************************************************************************/
  43.  
  44.  
  45. #define MAXGRADPENS 4
  46.  
  47.  
  48. /****************************************************************************/
  49.  
  50.  
  51. extern struct Library *DOSBase;
  52. extern struct Library *SysBase;
  53. struct Library        *IntuitionBase;
  54. struct Library        *GfxBase;
  55. struct Library        *ColorWheelBase;
  56. struct Library        *GradientSliderBase;
  57.  
  58.  
  59. /****************************************************************************/
  60.  
  61.  
  62. VOID main(VOID)
  63. {
  64. struct Screen        *screen;
  65. struct Window        *window;
  66. struct IntuiMessage  *intuiMsg;
  67. struct Gadget        *wheel;
  68. struct Gadget        *slider;
  69. struct ColorWheelRGB  rgb;
  70. struct ColorWheelHSB  hsb;
  71. BOOL                  quit;
  72. ULONG                 colortable[3];
  73. WORD                  pens[16];
  74. WORD                  numPens;
  75. WORD                  i;
  76.  
  77.     if (IntuitionBase      = OpenLibrary("intuition.library",39))
  78.     {
  79.         GfxBase            = OpenLibrary("graphics.library",39);
  80.         ColorWheelBase     = OpenLibrary("gadgets/colorwheel.gadget",39);
  81.         GradientSliderBase = OpenLibrary("gadgets/gradientslider.gadget",39);
  82.     }
  83.  
  84.     if (IntuitionBase && GfxBase && ColorWheelBase && GradientSliderBase)
  85.     {
  86.         if (screen = OpenScreenTags(NULL,SA_Depth,         4,
  87.                                          SA_LikeWorkbench, TRUE,
  88.                                          SA_Title,         "WheelGrad",
  89.                                          TAG_DONE))
  90.         {
  91.             /* get the RGB components of color 0 */
  92.             GetRGB32(screen->ViewPort.ColorMap,0,1,colortable);
  93.             rgb.cw_Red   = colortable[0];
  94.             rgb.cw_Green = colortable[1];
  95.             rgb.cw_Blue  = colortable[2];
  96.  
  97.             /* now convert the RGB values to HSB, and max out B component */
  98.             ConvertRGBToHSB(&rgb,&hsb);
  99.             hsb.cw_Brightness = 0xffffffff;
  100.  
  101.             numPens = 0;
  102.             while (numPens < MAXGRADPENS)
  103.             {
  104.                 hsb.cw_Brightness = 0xffffffff - ((0xffffffff / MAXGRADPENS) * numPens);
  105.                 ConvertHSBToRGB(&hsb,&rgb);
  106.  
  107.                 pens[numPens] = ObtainPen(screen->ViewPort.ColorMap,-1,
  108.                                  rgb.cw_Red,rgb.cw_Green,rgb.cw_Blue,PEN_EXCLUSIVE);
  109.  
  110.                 if (pens[numPens] == -1)
  111.                     break;
  112.  
  113.                 numPens++;
  114.             }
  115.             pens[numPens] = ~0;
  116.  
  117.             /* Create gradient slider and colorwheel gadgets */
  118.             slider = (struct Gadget *)NewObject(NULL,"gradientslider.gadget",
  119.                                                 GA_Top,        50,
  120.                                                 GA_Left,       177,
  121.                                                 GA_Width,      20,
  122.                                                 GA_Height,     100,
  123.                                                 GRAD_PenArray, pens,
  124.                                                 PGA_Freedom,   LORIENT_VERT,
  125.                                                 TAG_END);
  126.  
  127.             wheel = (struct Gadget *)NewObject(NULL, "colorwheel.gadget",
  128.                                                GA_Top,               50,
  129.                                                GA_Left,              50,
  130.                                                GA_Width,             120,
  131.                                                GA_Height,            100,
  132.                                                WHEEL_Red,            colortable[0],
  133.                                                WHEEL_Green,          colortable[1],
  134.                                                WHEEL_Blue,           colortable[2],
  135.                                                WHEEL_Screen,         screen,
  136.                                                WHEEL_GradientSlider, slider,
  137.                                                GA_FollowMouse,       TRUE,
  138.                                                GA_Previous,          slider,
  139.                                                TAG_END);
  140.  
  141.  
  142.             if (slider && wheel)
  143.             {
  144.                 if (window = OpenWindowTags(NULL,WA_Height,       200,
  145.                                                  WA_Width,        400,
  146.                                                  WA_CustomScreen, screen,
  147.                                                  WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEMOVE,
  148.                                                  WA_SizeGadget,   TRUE,
  149.                                                  WA_DragBar,      TRUE,
  150.                                                  WA_CloseGadget,  TRUE,
  151.                                                  WA_Gadgets,      slider,
  152.                                                  WA_Activate,     TRUE,
  153.                                                  TAG_DONE))
  154.                 {
  155.                     quit = FALSE;
  156.                     while (!quit)
  157.                     {
  158.                         WaitPort(window->UserPort);
  159.                         intuiMsg = (struct IntuiMessage *)GetMsg(window->UserPort);
  160.  
  161.                         switch (intuiMsg->Class)
  162.                         {
  163.                             case IDCMP_CLOSEWINDOW: quit = TRUE;
  164.                                                     break;
  165.  
  166.                             case IDCMP_MOUSEMOVE:
  167.                                  /* Change gradient slider color each time
  168.                                   * colorwheel knob is moved.  This is one
  169.                                   * method you can use.
  170.                                   */
  171.  
  172.                                  /* Query the colorwheel */
  173.                                  GetAttr(WHEEL_HSB,wheel,(ULONG *)&hsb);
  174.  
  175.                                  i = 0;
  176.                                  while (i < numPens)
  177.                                  {
  178.                                      hsb.cw_Brightness = 0xffffffff - ((0xffffffff / numPens) * i);
  179.                                      ConvertHSBToRGB(&hsb,&rgb);
  180.  
  181.                                      SetRGB32(&screen->ViewPort,pens[i],rgb.cw_Red,rgb.cw_Green,rgb.cw_Blue);
  182.                                      i++;
  183.                                  }
  184.                                  break;
  185.                         }
  186.                         ReplyMsg(intuiMsg);
  187.                     }
  188.                     CloseWindow(window);
  189.                 }
  190.             }
  191.  
  192.             /* Get rid of the gadgets */
  193.             DisposeObject(wheel);
  194.             DisposeObject(slider);
  195.  
  196.             /* Always release the pens */
  197.             while (numPens > 0)
  198.             {
  199.                 numPens--;
  200.                 ReleasePen(screen->ViewPort.ColorMap,pens[numPens]);
  201.             }
  202.  
  203.             CloseScreen(screen);
  204.         }
  205.     }
  206.  
  207.     if (IntuitionBase)
  208.     {
  209.         CloseLibrary(GradientSliderBase);
  210.         CloseLibrary(ColorWheelBase);
  211.         CloseLibrary(GfxBase);
  212.         CloseLibrary(IntuitionBase);
  213.     }
  214. }
  215.